﻿

using System;
using System.Collections.Generic;
using System.Data;
using BusinessObjects.BussinessDictionary;
using DataObjects.Shared;
using DataObjects.StandartInterfaces.Dictionary;

namespace DataObjects.ADO.SqlServer.Dictionary
{

    /// <summary>
    /// 
    /// </summary>
    public class SqlServerUnitDao : IUnitDao
    {
        /// <summary>
        /// Takes the specified employ test.
        /// </summary>
        /// <param name="unit">The employ test.</param>
        /// <returns></returns>
        private static object[] Take(Unit unit)
        {
            return new object[]  
            {
                 "@UnitID", unit.UnitId,
                "@Code", unit.Code,
                "@Grade", unit.Grade,
                "@Address", unit.Address,
                "@HasParent", unit.HasParent,
                "@ParentID", unit.ParentId,
                "@PhoneNumber", unit.PhoneNumber,
                "@Name", unit.Name,
                "@IsActive", unit.IsActive,
                "@ByManagement", unit.ByManagement
            };
        }

        private static object[] Take(int unitId)
        {
            return new object[]  
            {
                "@UnitID", unitId
            };
        }
        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, Unit> Make = reader =>
          new Unit
          {
              UnitId = reader["UnitId"].AsId(),
              Code = reader["Code"].AsString(),
              Grade = reader["Grade"].AsInt(),
              Address = reader["Address"].AsString(),
              HasParent = reader["HasParent"].AsBool(),
              ParentId = reader["ParentId"].AsString().AsInteger(),
             
              Name = reader["Name"].AsString(),
              IsActive = reader["IsActive"].AsBool(),
              PhoneNumber = reader["PhoneNumber"].AsString(),
              ByManagement = reader["ByManagement"].AsString()
          };

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="unitId">The unit identifier.</param>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public List<Unit> GetPaged(int unitId, int currentPage)
        {
            const string sql = @"uspGetPaged_Unit";

            object[] parms = { "@WhereClause", null, "@OrderBy", " UnitID ASC", "@PageIndex", currentPage-1 };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// </summary>
        /// <param name="unitId"></param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public int GetPageSize()
        {
            const string sql = @"uspGetPageSize_Unit";
            object[] parms = { "@WhereClause", null };
            return Db.GetCount(sql, CommandType.StoredProcedure, parms);
        }

       

       

        /// <summary>
        /// Inserts the unit.
        /// </summary>
        /// <param name="unit">The unit.</param>
        /// <returns></returns>
        public string InsertUnit(Unit unit)
        {
            const string sql = "uspInsert_Unit";
            return Db.Insert(sql, CommandType.StoredProcedure, Take(unit));
        }

        /// <summary>
        /// Updates the unit.
        /// </summary>
        /// <param name="unit">The unit.</param>
        /// <returns></returns>
        public string UpdateUnit(Unit unit)
        {
            const string sql = "uspUpdate_Unit";
            return Db.Update(sql, CommandType.StoredProcedure, Take(unit));
        }

        /// <summary>
        /// Deletes the unit.
        /// </summary>
        /// <param name="unit">The unit.</param>
        /// <returns></returns>
        public string DeleteUnit(Unit unit)
        {
            const string sql = @"uspDelete_Unit";
            object[] parms = { "@UnitID", unit.UnitId };
            return Db.Delete(sql, CommandType.StoredProcedure, parms);
        }





        public Unit GetUnit(int unitId)
        {
            const string sql = @"uspGet_Unit_ByID";
            object[] parms = { "@UnitID", unitId };
            return Db.Read(sql, CommandType.StoredProcedure,Make, parms);
        }

     

 

        public IList<Unit> GetUnitsByParentId(int parentId)
        {
            const string sql = @"uspGet_Unit_ByCondition";

            object[] parms = { "@WhereClause", " WHERE ParentID = " + parentId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        public long GetSequence()
        {
            return Db.GetCount("SELECT NEXT VALUE FOR [dbo].[SEQ_Unit_UnitID]");
        }

        public List<Unit> GetUnits()
        {
            const string sql = @"uspGetAll_Unit";
            return Db.ReadList(sql, CommandType.StoredProcedure, Make);
        }

        public List<Unit> GetUnits(int isActive)
        {
            const string sql = @"uspGet_Unit_ByCondition";

            object[] parms = { "@WhereClause", " WHERE IsActive = " + isActive };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }


        public Unit GetUnitByUserID(int userId)
        {
            const string sql = @"uspGet_Unit_ByID";
            object[] parms = { "@UserID", userId }; 
            return Db.Read(sql, CommandType.StoredProcedure, Make, parms);
        }
    }
}
